﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Scripting;
using Microsoft.Scripting.Ast;
using Microsoft.Linq.Expressions;

namespace DlrBasic.Parser.Ast
{
    class Randomize : Statement
    {
        DlrBasicExpression expression;

        public Randomize(SourceSpan span, DlrBasicExpression expression)
            : base(span)
        {
            this.expression = expression;
        }


        protected internal override Expression Generate(DlrBasicGenerator generator, StateMemberGenerator stateParameter, LambdaBuilder builder)
        {
            Expression randomizeCall;
            if (expression == null)
            {
                randomizeCall = Expression.Call(typeof(DlrBasicHelpers).GetMethod("RandomizeWithInput"),
                    Utils.CodeContext());
            }
            else
            {
                randomizeCall = Expression.Call(typeof(DlrBasicHelpers).GetMethod("Randomize"),
                    TypeConversionUtils.BuildConversionTo(typeof(double), expression.Generate(generator)));
            }
            return Expression.Block(randomizeCall, base.Generate(generator, stateParameter, builder));
        }
    }
}
